package com.qf.controller;

import com.qf.pojo.Account;
import com.qf.pojo.Result;
import com.qf.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ShenZhuoPing
 * @version V1.0
 * @Project vue_ssm
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/22 11:30
 */
@RestController
@RequestMapping("account")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @RequestMapping("/addAccount")
    public Result addAccount(@RequestBody Account account){
        try {
            accountService.addAccount(account);
            return new Result(true,null,"添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null,"添加用户失败");
        }
    }
    @RequestMapping("/findAll")
    public Result findAccount(){
        try {
            List<Account> accountList = accountService.findAll();
            return new Result(true,accountList,"查询所有账户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null,"查询所有账户失败");
        }
    }
    @RequestMapping("/{id}")
    public Result deleteAccount(@PathVariable("id") Integer id){
        try {
            accountService.deleteById(id);
            return new Result(true,null,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,null,"删除失败");
        }
    }
    @RequestMapping("/update/{id}")
    public Result selectById(@PathVariable("id") Integer id){
        try {
            Account account = accountService.findById(id);
            return new Result(true,account,"返回成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,null,"返回失败");
        }
    }
    @RequestMapping("/update")
    public Result updateAccount(@RequestBody Account account){
        try {
            accountService.update(account);
            return new Result(true,null,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null,"修改失败");
        }

    }
}
